<?php

// +----------------------------------------------------------------------
// | Wechat Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-wechat
// | github 代码仓库：https://github.com/zoujingli/think-plugs-wechat
// +----------------------------------------------------------------------

namespace app\data\service;

use think\admin\Exception;
use think\admin\extend\JsonRpcClient;
use think\admin\Library;
use think\admin\storage\LocalStorage;
use think\exception\HttpResponseException;

use AlibabaCloud\SDK\Iot\V20180120\Iot;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;
use AlibabaCloud\Tea\Exception\TeaError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Iot\V20180120\Models\SetDevicePropertyRequest;
use AlibabaCloud\SDK\Iot\V20180120\Models\InvokeThingServiceRequest;
use AlibabaCloud\SDK\Iot\V20180120\Models\QueryDevicePropertyStatusRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class IotThingService
{
    private $client;
    public function __construct( )
    {
        $config = new Config([]);
        $config->accessKeyId = config('iot.accessKeyId');// 您的AccessKey ID。
        $config->accessKeySecret = config('iot.accessKeySecret');// 您的AccessKey Secret。
        $config->regionId = config('iot.regionId');// 您的可用区ID。
        $this->client = new Iot($config);
    }
    //设置物模型属性
    public function setProp($iotId,$item){
        try {
            $data=[
                'items'=>$item,
                'iotInstanceId'=>config('iot.IotInstanceId'),
                'iotId'=>$iotId
            ];
            $request = new SetDevicePropertyRequest($data);
            $response =  $this->client->SetDeviceProperty($request);
            $res['err_code']=0;
            $res['data'] = $response;
        } catch (TeaUnableRetryError $e) {
            $res['err_code']=1;
            $res['data'] = $e->getMessage();
        }
        return $res;
    }
    //调用服务
    public function setService($iot_id,$title,$args='{}'){
        $runtime = new RuntimeOptions([]);
        try {
            $data=[
                'identifier'=>$title,
                'iotInstanceId'=>config('iot.IotInstanceId'),
                'iotId'=>$iot_id,
                'args'=>$args
            ];
            $request = new InvokeThingServiceRequest($data);
            //$response =  $this->client->InvokeThingService($request);
            $response = $this->client->invokeThingServiceWithOptions($request,$runtime);
            $res['err_code']=0;
            $res['data'] = $response;
        } catch (TeaUnableRetryError $e) {
            $res['err_code']=1;
            $res['data'] = $e->getMessage();
        }
        return $res;
    }
    //查询指定设备属性
    public function getProp($data){
        try {
            //var_dump($data);
            $request = new QueryDevicePropertyStatusRequest($data);
            $response =  $this->client->QueryDevicePropertyStatus($request);
            $res['err_code']=0;
            $res['data'] = $response;
        } catch (TeaUnableRetryError $e) {
            $res['err_code']=1;
            $res['data'] = $e->getMessage();
        }
        return $res;
    }
}
